package 经典100题;

import java.util.Stack;

public class 反转链表 {

    public static void main(String[] args) {
        Node node = createNodeList();
        Node newNode = reverseNodeList(node);

        System.out.println("翻转后：");
        while (newNode != null){
            System.out.println(newNode.getValue());
            newNode = newNode.tail;
        }
    }
    public static Node reverseNodeList(Node node){

        Node fast = node.tail;
        Node slow = node;
        slow.tail = null;
        while (fast != null){
            Node temp = fast.tail;
            fast.tail = slow;
            slow = fast;
            fast = temp;
        }
        return slow;
    }
    public static Node createNodeList(){
        Node head = new Node();
        Node prev = head;
        head.setValue(1);
        for (int i = 2; i < 10; i++) {
            Node node = new Node();
            node.setValue(i);
            //node.setPrevN(prev);
            prev.setTail(node);
            prev = node;
        }
        return head;
    }
    public static class Node {
        int value;
        private Node prevN;
        private Node tail;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getPrevN() {
            return prevN;
        }

        public void setPrevN(Node prevN) {
            this.prevN = prevN;
        }

        public Node getTail() {
            return tail;
        }

        public void setTail(Node tail) {
            this.tail = tail;
        }
    }
}
